package com.ws.shop.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.stereotype.Repository;

import com.ws.shop.bean.Page;
import com.ws.shop.dao.PackageDao;
import com.ws.shop.entity.Packagz;
import com.ws.shop.util.JdbcUtil;

@Repository(value="packageDao")
public class PackageDaoImpl extends BaseDaoImpl implements PackageDao {

	public void queryPackages(final Page page){
		this.execute(new ConnectionCallback() {
			@SuppressWarnings("unchecked")
			public Object doInConnection(Connection conn) throws SQLException,
					DataAccessException {
				Statement stmt = null;
				ResultSet rs = null;
				try {
					String s = "SELECT COUNT(1) FROM jvf_packagz ";
					stmt = conn.createStatement();
					rs = stmt.executeQuery(s);
					long totalnum = 0;
					if (rs.next()) {
						totalnum = rs.getLong(1);
						page.setTotalnum(totalnum);
					}
					JdbcUtil.close(rs, stmt, null);
					if (totalnum > 0) {
                        StringBuffer sbuffer = new StringBuffer();
                        sbuffer.append("SELECT P.id,P.amount,P.beat,P.pointb,P.pointc,P.path,A.path AS apath FROM jvf_packagz AS P JOIN jvf_accessory AS A ON P.path = A.id ");
						rs = stmt.executeQuery(sbuffer.append(" limit ")
								.append(page.getStartrownum()).append(",")
								.append(page.getPagesize()).toString());
						while (rs.next()) {
							Packagz packages = new Packagz();
							packages.setId(rs.getInt(1));
							packages.setAmount(rs.getInt(2));
							packages.setBeat(rs.getInt(3));
							packages.setPointb(rs.getInt(4));
							packages.setPointc(rs.getInt(5));
							packages.setPath(rs.getInt(6));
							packages.setApth(rs.getString(7));
							page.addData(packages);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					JdbcUtil.close(rs, stmt, null);
				}
				return null;
			}
		});
	}
}
